﻿
namespace AspNetCoreWebMiddleware.Middleware
{
    public class HtmlMiddlewares
    {

        private RequestDelegate next;

        public HtmlMiddlewares(RequestDelegate requestDelegate)
        {
            this.next = requestDelegate;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            // 1、获取请求路径 /js  /html /css /
            var path = context.Request.Path.Value;
            if (path.Equals("/html"))
            {
                await ResposeHtml(context);
            }
            else
            {
                // 2、给下一个中间件
                await next(context);
            }
        }

        public static async Task ResposeHtml(HttpContext httpContext)
        {
            // 1、设置内容类型
            httpContext.Response.ContentType = "text/html";
            // 2、获取html文件路径
            var filepath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html");
            // 3、读取文件内容
            var htmlContent = await File.ReadAllTextAsync(filepath);
            // 4、响应输出
            await httpContext.Response.WriteAsync(htmlContent);
        }
    }
}
